package com.wontube.core.conf.dogs;

import java.io.File;
import java.util.Date;

import com.wontube.client.extension.utils.FileUtils;
import com.wontube.core.srv.interval.ITask;



/**
 * @author yuechao
 *
 */
public abstract class FileWatchDog implements ITask {
	private long lastModif = 0;
	protected File file;

	public FileWatchDog(String confPath) {
		String filename = FileUtils.combinePath(confPath, this.getFilename());
		this.file = new File(filename);
	}

	protected abstract String getFilename();
	protected abstract void doOnChange();

	public final FileWatchDog checkAndConfigure() {
		boolean fileExists;
		try {
			fileExists = file.exists();
		} catch (SecurityException e) {
			return this;
		}

		if (fileExists) {
			long l = file.lastModified();
			if (l > lastModif) { 
				this.lastModif = l; 
				this.doOnChange();
			}
		}
		
		return this;
	}

	@Override
	public void execute(Date time) {
		this.checkAndConfigure();
	}
}
